var ghost_isBuildingSmtg = false;
var ghost_item = null;
var ghost_curTile = null;
var ghost_prevTile = null;

var GhostBuilding = function(itemType)
{
    this.canBuild = true;

    this.img = null;
    this.img_ok = null;
    this.img_fail = null;

    this.type = itemType;
    this.newBuilding = null;

    switch(this.type) {
        case(BUILDINGS.SILO):
            this.img_ok = g_siloImg_ok;
            this.img_fail = g_siloImg_fail;
            this.newBuilding = new Silo();
            break;

        case (BUILDINGS.BARN):
            this.img_ok = g_barnImg_ok;
            this.img_fail = g_barnImg_fail;
            this.newBuilding = new Barn();
            break;

        case(BUILDINGS.COLDSTORAGE):
            this.img_ok = g_coldstorageImg_ok;
            this.img_fail = g_coldstorageImg_fail;
            this.newBuilding = new ColdStorage();
            break;

        case(CROPS.CORN):
            this.img_ok = g_corn_ok;
            this.img_fail = g_corn_fail;
            this.newBuilding = new Corn();
            break;

        case(CROPS.TOMATO):
            this.img_ok = g_tomato_ok;
            this.img_fail = g_tomato_fail;
            this.newBuilding = new Tomato();
            break;

        case(CROPS.WHEAT):
            this.img_ok = g_wheat_ok;
            this.img_fail = g_wheat_fail;
            this.newBuilding = new Wheat();
            break;

        default:
            break;
    }
}

GhostBuilding.prototype.drawGhost = function () {
    this.canBuild = true;

    switch(this.type) {
        case(BUILDINGS.SILO) :
        case(BUILDINGS.BARN) :
        case(BUILDINGS.COLDSTORAGE) :
            //On récupere la liste de tile sous le fantôme, pour test un éventuel placement
            var tileList = this.newBuilding.getTilesUnder(ghost_curTile);



            for(var i=0; i<tileList.length; i++) {
                //Si le bâtiment dépasse les limites de la carte, on ne dessine pas le fantome
                if(tileList[i] == "OVERFLOW_UP" || tileList[i] == "OVERFLOW_RIGHT")
                    return;
                if(tileList[i] == false || tileList[i] == undefined)
                    this.canBuild = false;
                else if( (tileList[i].canPass != 1)
                    || (tileList[i] == findCurHeroTile(hero))
                    || (tileList[i].heroId != hero.id))
                    this.canBuild = false;
            }
            break;

        case(CROPS.CORN):
        case(CROPS.TOMATO):
        case(CROPS.WHEAT):
            if( (ghost_curTile.canPass != 1)
                || (ghost_curTile == findCurHeroTile(hero))
                || (ghost_curTile.heroId != hero.id))
                this.canBuild = false;
            break;
    }

    if(this.canBuild == true)
        this.img = this.img_ok;
    else
        this.img = this.img_fail;


    switch(this.type) {
        //offset de hauteur du dessin pour les barns & coldstorages
        case(BUILDINGS.BARN):
        case(BUILDINGS.COLDSTORAGE):
            c.drawImage(this.img,
                ghost_curTile.x + camera.x,
                ghost_curTile.y + camera.y - (defaultTileHeight / 2));
            break;

        //Pas d'offset pour les constructions / plantations d'1x1 tile
        case(BUILDINGS.SILO):
        case(CROPS.CORN):
        case(CROPS.TOMATO):
        case(CROPS.WHEAT):
            c.drawImage(this.img,
                ghost_curTile.x + camera.x,
                ghost_curTile.y + camera.y);
            break;

        default:
            break;
    }


}

GhostBuilding.prototype.tryCreateBuilding = function() {
    if(this.canBuild == false) {
        return false;
    }

    var price;

    switch(this.type) {
        case(BUILDINGS.SILO) :
            this.newBuilding = new Silo(-1, ghost_curTile);
            break;
        case(BUILDINGS.BARN) :
            this.newBuilding = new Barn(-1, ghost_curTile);
            break;
        case(BUILDINGS.COLDSTORAGE) :
            this.newBuilding = new ColdStorage(-1, ghost_curTile);
            break;

        case(CROPS.CORN):
            this.newBuilding = new Corn(-1, ghost_curTile);
            break;
        case(CROPS.TOMATO):
            this.newBuilding = new Tomato(-1, ghost_curTile);
            break;
        case(CROPS.WHEAT):
            this.newBuilding = new Wheat(-1, ghost_curTile);
            break;
    }

    price = this.newBuilding.cost;
    if(hero.gold < price) {
        gameMsg('Not enough gold !');
        return false;
    } else {
        switch(this.type) {
            case(BUILDINGS.SILO):
            case(BUILDINGS.BARN):
            case(BUILDINGS.COLDSTORAGE):
                this.newBuilding.setupBuilding();
                break;
            case(CROPS.TOMATO):
            case(CROPS.CORN):
            case(CROPS.WHEAT):
                this.newBuilding.setupCrop();
        }
        hero.gold -= price;
        updHeroStats();
        return true;
    }
};
